package cn.ll.filter;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import cn.ll.po.User;
import cn.ll.vo.LoginUser;

public class UserFilter extends HttpServlet implements Filter {

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain filterChain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		String uri = request.getRequestURI();
		String url = request.getServletPath();
		String contextPath = request.getContextPath();
		String[] excludeuri = { "/user/login.jsp", "/user.do:login:logout" };
		String method = request.getParameter("m");

		//不需要做登录验证的请求
		int excludeuri_size = excludeuri.length;
		for (int i = 0; i < excludeuri_size; i++) {
			String[] tmp = excludeuri[i].split(":");

			if (tmp[0].equalsIgnoreCase(url)) {
				if (tmp.length > 1) {
					for (int index = 1; index < tmp.length; index++) {
						if (method.equalsIgnoreCase(tmp[index])) {
							filterChain.doFilter(req, res);
							return;
						}
					}
				} else {
					filterChain.doFilter(req, res);
					return;
				}

			}
		}
		LoginUser current_user = null;
		current_user = (LoginUser) request.getSession().getAttribute("CURRENT_USER");

		if (current_user == null) {
			//        	response.setContentType("text/xml; charset=" + "UTF-8");
			//    		PrintWriter writer = null;
			//    		String rs = "needlogin";
			//    		try {
			//    			writer = response.getWriter();
			//    		} catch (IOException e) {
			//    			e.printStackTrace();
			//    		}
			//
			//    		writer.write(rs);
			//    		writer.close();
			if (url.equalsIgnoreCase("/index.jsp")) {
				response.sendRedirect(contextPath + "/user/login.jsp");
			} else {
				Map rs = new HashMap();
				rs.put("success", false);
				rs.put("appStatus", "0");
				rs.put("msg", "Need login");
				
				JSONObject object = JSONObject.fromObject(rs);
				sendAjaxResponseText(object.toString(), request, response);
			}
			return;
		} else {
			filterChain.doFilter(req, res);
			return;
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

	private void sendAjaxResponseText(String rs, HttpServletRequest request,
			HttpServletResponse response) {
		response.setContentType("text/xml; charset=" + "UTF-8");
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}

		writer.write(rs);
		writer.close();
	}
}
